package edu.maplef.bookwild.action;

import java.util.Map;

import org.apache.struts2.interceptor.CookiesAware;
import org.apache.struts2.interceptor.ParameterAware;

import com.opensymphony.xwork2.ActionSupport;

import edu.maplef.bookwild.config.Constants;
import edu.maplef.bookwild.schema.Buddy;
import edu.maplef.bookwild.utils.StrCharUtil;
import edu.maplef.bookwild.utils.network.HttpClient;

public class BaseAction extends ActionSupport implements CookiesAware,
		ParameterAware {
	private static final long serialVersionUID = -6876344390630259662L;

	protected Map<String, String> cookies = null;
	protected Map<String, String[]> parameters = null;
	protected HttpClient hc = new HttpClient();
	Buddy currentUser;
	String user_id;

	public void validate() {

	}

	@Override
	public void setCookiesMap(Map<String, String> arg0) {
		this.cookies = arg0;
	}

	@Override
	public void setParameters(Map<String, String[]> arg0) {
		this.parameters = arg0;
	}

	/**
	 * 获取指定的cookie值，没有找到返回null
	 * @param name  
	 * @return value
	 */
	public String getCookieValueByName(String name) {
		if (this.cookies == null)
			return null;
		return cookies.get(name);
	}

	/**
	 * 获取指定的parameter值，没有找到返回null
	 * @param name  
	 * @return value
	 */
	public String getParameterByName(String name) {
		if (this.parameters == null)
			return null;
		String[] values = parameters.get(name);
		if (values == null || values.length == 0)
			return null;
		return StrCharUtil.parseStringArray(values);
	}

	/**
	 * 获取已登陆的用户
	 * @return
	 */
	public Buddy getLoginUser() {
		return new Buddy(getParameterByName(Constants.PARAM_XN_SIG_USER));
	}

	/**
	 * 判断用户是否登陆
	 * @return
	 */
	public boolean isLogin() {
		if (!StrCharUtil
				.isNullOrEmpty(getParameterByName(Constants.PARAM_XN_SIG_USER)))
			return true;
		return false;
	}
}
